//
//  AppDelegate.m
//  InSuger
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 Dior_3g. All rights reserved.
//

#import "AppDelegate.h"
#import "HomePageTableViewController.h"
#import "FinderViewController.h"
#import "SearchCollectionViewController.h"
#import "MineTableViewController.h"

#import "BmbViewController.h"

@interface AppDelegate ()<WXApiDelegate, WeiboSDKDelegate>


@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    

    self.tab = [[UITabBarController alloc] init];
    self.window.rootViewController = self.tab;

    self.window.backgroundColor = [UIColor whiteColor];
    
    UITabBarController *tab = [[UITabBarController alloc] init];
    self.window.rootViewController = tab;
    
    
    HomePageTableViewController *homeVC = [[HomePageTableViewController alloc] init];
    UINavigationController *homeNAVC = [[UINavigationController alloc] initWithRootViewController:homeVC];
    homeNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"home1.png"] selectedImage:[UIImage imageNamed:@"home2.png"]];
    
    
    FinderViewController *finderVC = [[FinderViewController alloc] init];
    UINavigationController *finderNAVC = [[UINavigationController alloc] initWithRootViewController:finderVC];
    finderNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:[UIImage imageNamed:@"finder1.png"] selectedImage:[UIImage imageNamed:@"finder2.png"]];
    
    
    SearchCollectionViewController *searchVC = [[SearchCollectionViewController alloc] init];
    UINavigationController *searchNAVC = [[UINavigationController alloc] initWithRootViewController:searchVC];
    searchNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"搜索" image:[UIImage imageNamed:@"search1.png"] selectedImage:[UIImage imageNamed:@"search2.png"]];
    
    
    MineTableViewController *mineVC = [[MineTableViewController alloc] init];
    UINavigationController *mineNAVC = [[UINavigationController alloc] initWithRootViewController:mineVC];
    mineNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"mine1.png"] selectedImage:[UIImage imageNamed:@"mine2.png"]];
    
    
    self.tab.viewControllers = @[homeNAVC, finderNAVC, searchNAVC, mineNAVC];
    
    
    //获取沙盒中的版本号
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *key = (NSString *)kCFBundleVersionKey;
    NSString *sandBoxVersion = [defaults valueForKeyPath:key];
    // 获取当前版本号
    NSDictionary *md = [NSBundle mainBundle].infoDictionary;
    NSString *currentVersion = md[key];
    
    NSLog(@"%@", NSHomeDirectory());
    if ([currentVersion compare:sandBoxVersion] == NSOrderedDescending)
    {
        NSLog(@"ss ");
        // 存储当前版本号
        [defaults setObject:currentVersion forKey:key];
        //锁定当前线程
        [defaults synchronize];
        // 第一次使用当前版本  显示引导页
        [self showGuidePage];
    }
    else
    {
        NSLog(@"sssdsf");
        // 不是第一次使用当前版本  显示主页面
        [self.window setRootViewController:self.tab];
        
    }
    
    //  微信的AppID
    [WXApi registerApp:@"wxa4bb29e0070ecb6d"];
    //  微博的AppID
    [WeiboSDK enableDebugMode:YES];
    [WeiboSDK registerApp:@"4017038878"];
    
    
    return YES;
}


- (void)showGuidePage
{
    BmbViewController *scroller = [[BmbViewController alloc] init];
    [self.window setRootViewController:scroller];
}

//  分享后返回时回调启动的应用

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
    
    return [WXApi handleOpenURL:url delegate:self];
    return [WeiboSDK handleOpenURL:url delegate:self];
    
}

/*! @brief 收到一个来自微信的请求，第三方应用程序处理完后调用sendResp向微信发送结果
 *
 * 收到一个来自微信的请求，异步处理完成后必须调用sendResp发送处理结果给微信。
 * 可能收到的请求有GetMessageFromWXReq、ShowMessageFromWXReq等。
 * @param req 具体请求内容，是自动释放的
 */
-(void) onReq:(BaseReq*)req {
    
}

/*! @brief 发送一个sendReq后，收到微信的回应
 *
 * 收到一个来自微信的处理结果。调用一次sendReq后会收到onResp。
 * 可能收到的处理结果有SendMessageToWXResp、SendAuthResp等。
 * @param resp具体的回应内容，是自动释放的
 */
-(void) onResp:(BaseResp *)resp {
    
}

/**
 收到一个来自微博客户端程序的请求
 
 收到微博的请求后，第三方应用应该按照请求类型进行处理，处理完后必须通过 [WeiboSDK sendResponse:] 将结果回传给微博
 @param request 具体的请求对象
 */
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request
{
    
}

/**
 收到一个来自微博客户端程序的响应
 
 收到微博的响应后，第三方应用可以通过响应类型、响应的数据和 WBBaseResponse.userInfo 中的数据完成自己的功能
 @param response 具体的响应对象
 */
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
    
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
